package cn.itcast.service.cargo.impl;


import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
@Transactional
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ContractProductDao contractProductDao;


    @Override
    public Contract findById(String id) {
        return contractDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Contract contract) {
        contractDao.insertSelective(contract);

    }

    @Override
    public void update(Contract contract) {
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {//合同id
        //根据合同id删除附件
        extCproductDao.deleteByContractId(id);
        //根据合同id删除货物
        contractProductDao.deleteByContractId(id);
        contractDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractExample example, int page, int size) {

        PageHelper.startPage(page,size);

        List<Contract> list = contractDao.selectByExample(example);

        return new PageInfo<Contract>(list,5);
    }


    //根据船期查询并封装为vo对象
    public List<ContractProductVo> findContractProductVoByShipTime(String inputDate,String companyId){
        return contractDao.findContractProductVoByShipTime(inputDate,companyId);
    }
}
